package cn.cdutetc.foxwatch.serveice.imp;

import cn.cdutetc.foxwatch.common.ExceptionEnum;
import cn.cdutetc.foxwatch.exception.MyException;
import cn.cdutetc.foxwatch.mapper.UserMapper;
import cn.cdutetc.foxwatch.pojo.UserPojo;
import cn.cdutetc.foxwatch.serveice.UserService;
import cn.cdutetc.foxwatch.utils.JwtUtil;
import cn.cdutetc.foxwatch.utils.Md5Util;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.micrometer.common.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Transactional
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPojo> implements UserService {
    @Override
    public Boolean register(UserPojo userPojo, String rePassword) throws MyException {
        String username = userPojo.getUserName();
        String password = userPojo.getPassword();
        String role = userPojo.getRole();
        //参数校验

        if (StringUtils.isEmpty(role) || StringUtils.isEmpty(password) || StringUtils.isEmpty(rePassword) || StringUtils.isEmpty(username)) {
            throw new MyException(500, ExceptionEnum.ParameterError.getDesc());
        }
        //密码校验
        if (!password.equals(rePassword)) {
            throw new MyException(500, ExceptionEnum.PasswordRepeat.getDesc());
        }
        //查询用户是否存在
        LambdaQueryWrapper<UserPojo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPojo::getUserName, username).eq(UserPojo::getRole, role);
        UserPojo user = this.baseMapper.selectOne(queryWrapper);
        //用户已存在
        if (user != null) {
            throw new MyException(500, ExceptionEnum.UserExist.getDesc());
        }
        //明文转密文
        String mdPsd = Md5Util.getMD5String(password);
        userPojo.setPassword(mdPsd);
        int i = this.baseMapper.insert(userPojo);
        return i == 1;
    }

    @Override
    public String login(UserPojo userPojo) throws MyException {
        String username = userPojo.getUserName();
        String password = userPojo.getPassword();
        String role = userPojo.getRole();
        //参数校验
        if (StringUtils.isEmpty(role) || StringUtils.isEmpty(password) || StringUtils.isEmpty(username)) {
            throw new MyException(500, ExceptionEnum.ParameterError.getDesc());
        }
        //查询用户是否存在
        LambdaQueryWrapper<UserPojo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPojo::getUserName, username).eq(UserPojo::getRole, role);

        UserPojo user = this.baseMapper.selectOne(queryWrapper);
        //用户不存在
        if (user == null) {
            throw new MyException(500, ExceptionEnum.UserNotExist.getDesc());
        }
        //密码校验
        String mdPsd = Md5Util.getMD5String(password);
        if (!mdPsd.equals(user.getPassword())) {
            throw new MyException(500, ExceptionEnum.PasswordError.getDesc());
        }
        //登录成功
        Map<String, Object> map = new HashMap<>();
        map.put("userId", user.getId());
        map.put("userName", user.getUserName());
        map.put("role", user.getRole());
        return JwtUtil.genToken(map);
    }
}
